Page({
    data: {
        userInfo: {},
        menuItems: [],
        switchButtonText: '',
        switchButtonClass: '',
        userRole: ''
    },

    onLoad() {
        this.loadUserInfo();
    },

    onShow() {
        this.loadUserInfo();
    },

    // 加载用户信息
    loadUserInfo() {
        const userRole = wx.getStorageSync('userRole');
        if (!userRole) {
            // 如果角色未选择，跳转到角色选择页面
            wx.redirectTo({
                url: '/pages/role-select/role-select'
            });
            return;
        }

        this.setData({
            userRole: userRole
        });

        if (userRole === 'student') {
            this.loadStudentProfile();
        } else if (userRole === 'teacher') {
            this.loadTeacherProfile();
        }
    },

    // 加载学生个人信息
    loadStudentProfile() {
        const storedUserInfo = wx.getStorageSync('studentInfo');
        const defaultUserInfo = {
            name: '张三',
            campus: '南校区', // 需要添加这个字段
            studentId: '2023001',
            class: '计算机科学与技术1班',
            phone: '138****8888',
            email: 'zhangsan@example.com',
            avatar: '/images/avatar-default.svg'
        };

        const studentMenuItems = [
            { name: '缴费记录', icon: '📋', url: '/pages/student/payment/payment?tab=1' },
            { name: '我的课程', icon: '📚', url: '/pages/student/courses/courses' },
            { name: '观看历史', icon: '🕒', url: '/pages/student/watch-history/watch-history' },
            { name: '电子票据', icon: '🧾', url: '/pages/student/receipt/receipt' },
            { name: '电子合同', icon: '📄', url: '/pages/student/contract/contract' }
        ];

        this.setData({
            userInfo: storedUserInfo || defaultUserInfo,
            menuItems: studentMenuItems,
            switchButtonText: '切换到老师身份',
            switchButtonClass: 'switch-role-btn'
        });
    },

    // 加载老师个人信息
    loadTeacherProfile() {
        const storedUserInfo = wx.getStorageSync('teacherInfo');
        const defaultUserInfo = {
            name: '李老师',
            campus: '北校区', // 需要添加这个字段
            teacherId: 'T2023001',
            department: '计算机科学与技术系',
            phone: '139****9999',
            email: 'liteacher@example.com',
            avatar: '/images/avatar-default.svg'
        };

        const teacherMenuItems = [
            { name: '学生管理', icon: '👥', url: '/pages/teacher/student-list/student-list' },
            { name: '课程管理', icon: '📚', url: '/pages/teacher/course-management/course-management' },
            { name: '通知发布', icon: '📢', url: '/pages/teacher/notice-management/notice-management' }
        ];

        this.setData({
            userInfo: storedUserInfo || defaultUserInfo,
            menuItems: teacherMenuItems,
            switchButtonText: '切换到学生身份',
            switchButtonClass: 'switch-role-btn-teacher'
        });
    },

    // 点击菜单项
    navigateToPage(e) {
        const url = e.currentTarget.dataset.url;
        // 检查页面是否存在，如果不存在则显示开发中提示
        if (url.includes('course-management') || url.includes('homework-management') ||
            url.includes('grade-management') || url.includes('attendance-management') ||
            url.includes('notice-management')) {
            wx.showModal({
                title: '功能提示',
                content: '该功能正在开发中，敬请期待',
                showCancel: false
            });
        } else {
            wx.navigateTo({
                url: url
            });
        }
    },

    // 编辑个人信息
    editProfile() {
        wx.showModal({
            title: '功能提示',
            content: '个人信息修改功能正在开发中',
            showCancel: false
        });
    },

    // 切换角色
    switchRole() {
        const currentRole = this.data.userRole;
        const targetRole = currentRole === 'student' ? 'teacher' : 'student';
        const targetRoleText = currentRole === 'student' ? '老师' : '学生';

        wx.showModal({
            title: '切换身份',
            content: `确定要切换到${targetRoleText}身份吗？`,
            confirmText: '确定',
            cancelText: '取消',
            success: (res) => {
                if (res.confirm) {
                    wx.setStorageSync('userRole', targetRole);
                    wx.showToast({
                        title: `已切换到${targetRoleText}身份`,
                        icon: 'success',
                        duration: 1500
                    });
                    setTimeout(() => {
                        this.loadUserInfo();
                    }, 1500);
                }
            }
        });
    }
}); 